﻿<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" Theme="WinXP_Silver" %>

<%@ Import Namespace="System.Data" %>

<script runat="server">
    Dim name As String = ""
    Dim items1 As items = New items
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
      
        If IsPostBack Then
            Response.Write("postback")
        End If
        
         
        Dim sql_first As String = "Select id_item,name,amount,description,id_image,id_type,id_location, date, active from items "
        
        Dim conection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString


        Dim SqlConnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        SqlConnection1.ConnectionString = conection_string
        SqlConnection1.Open()

        'Comando a enviar á base de dados
        Dim sqlComand1 As System.Data.SqlClient.SqlCommand
        sqlComand1 = New System.Data.SqlClient.SqlCommand(sql_first)



        Dim SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
        SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter(sql_first, SqlConnection1)

        Dim DataSet1 As New DataSet
        SqlDataAdapter1.Fill(DataSet1, "pesquisa")
        GridView1.DataSource = DataSet1.Tables("pesquisa")
        GridView1.DataBind()
        
        'primeiro id
        items1.select_from_db()
        
    End Sub
    
    Public Sub show_items_on_page()
        txt_id_item.Text = items1.id_items
        txt_id_location.Text = items1.id_location
        txt_id_image.Text = items1.id_image
        txt_id_type.Text = items1.id_type
        txt_amount.Text = items1.amount
        txt_bit.Text = items1.bit.ToString()
        txt_name.Text = items1.name.ToString()
        txt_date_a.Text = items1.date_a.ToString()
        txt_description.Text = items1.description.ToString()
        
    End Sub
    
    Public Sub cleanfields()
        txt_id_item.Text = ""
        txt_id_location.Text = ""
        txt_id_image.Text = ""
        txt_id_type.Text = ""
        txt_amount.Text = ""
        txt_bit.Text = ""
        txt_name.Text = ""
        txt_date_a.Text = ""
        txt_description.Text = ""
        
    End Sub

    Protected Sub btn_insert_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        
        formfields()
        lbl_info.Text = "Sucess!" + items1.items_insert()
        
        lbl_info.ForeColor = Drawing.Color.Green
        
    End Sub
     

    
    Public Sub formfields()
        items1.name = txt_name.Text
        items1.id_items = Integer.Parse(txt_id_item.Text).ToString()
        items1.amount = Integer.Parse(txt_amount.Text).ToString()
        If txt_bit.Text = "1" Then
            items1.bit = True
        Else
            items1.bit = False
            
        End If
        items1.date_a = Now ' Date.Parse(txt_date_a.Text)
        items1.id_image = Integer.Parse(txt_id_image.Text)
        items1.id_location = Integer.Parse(txt_id_location.Text)
        items1.id_type = Integer.Parse(txt_id_type.Text)
        items1.description = txt_description.Text
    End Sub
   




    Protected Sub btn_first_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        
        Dim sql_first As String = "Select top 1 id_item from items order by id_item ASC"
        
        Dim conection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString


        Dim SqlConnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        SqlConnection1.ConnectionString = conection_string
        SqlConnection1.Open()

        Dim SqlCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
        SqlCommand1.Connection = SqlConnection1
        SqlCommand1.CommandText = sql_first
        items1.id_items = Integer.Parse(SqlCommand1.ExecuteScalar())
        SqlConnection1.Close()
        
        'primeiro id
        items1.select_From_db()
        
        formfields()
    End Sub


    Protected Sub btn_next_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfields()
        lbl_info.Text = "Success" + items1.nextt()
        lbl_info.ForeColor = Drawing.Color.Orange
        
        txt_id_item.Text = items1.id_items
        txt_id_image.Text = items1.id_image
        txt_id_type.Text = items1.id_type
        txt_id_location.Text = items1.id_location
        
    End Sub


    Protected Sub btn_delete_Click1(ByVal sender As Object, ByVal e As System.EventArgs)
        
        items1.delete()
        
        formfields()
        lbl_info.Text = "Success! " + items1.delete()
        lbl_info.ForeColor = Drawing.Color.Orange
        cleanfields()
        
    End Sub

 
    Protected Sub btn_rewind_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Protected Sub btn_forward_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
       Page.ClientScript.RegisterStartupScript("".GetType(),"ClientScript", "alert('Mensagem');",True)
    End Sub
       
    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
        'numero da linha
        'Page.ClientScript.RegisterStartupScript("".GetType(), "ClientScript", "alert('id:" + GridView1.Rows(e.RowIndex).Cells(2).Text + "');", True)
        'falta apagar
        'items1.id_items = Integer.Parse(GridView1.Rows(e.RowIndex).Cells(2).Text)
        'items1.delete()
        'cleanfields()
        'variabletoformfield()

        jccc.utilidades.utilidades_java.mensagem(Page, jccc.utilidades.encriptacao.gerapassword(30))
        
        
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <style type="text/css">
        .style1
        {
            width: 1002px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <h1>
        Items</h1>
    <table width="100%">
        <tr>
            <td class="style1">
                <asp:Label ID="lbd_id_item" runat="server" Text="id_item"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_id_item" runat="server" Text="0" ReadOnly="true"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="lbl_name" runat="server" Text="Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="lbl_amount" runat="server" Text="Amount"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_amount" runat="server" Text="0" ReadOnly="true"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="lbl_description" runat="server" Text="Description"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_description" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="lbl_id_image" runat="server" Text="Id_image"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_id_image" runat="server" Text="0" ReadOnly="true"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="lbl_id_type" runat="server" Text="ID_Type"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_id_type" runat="server" Text="0" ReadOnly="true"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="lbl_id_location" runat="server" Text="ID_Location"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_id_location" runat="server" Text="0" ReadOnly="true"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="lbl_date_a" runat="server" Text="Date_a"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_date_a" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="lbl_bit" runat="server" Text="Bit"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_bit" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Button ID="btn_first" runat="server" Text="First" 
                    onclick="btn_first_Click"/>
            <asp:Button ID="btn_rewind" runat="server" Text="Back &quot;em falta&quot;" 
                    onclick="btn_rewind_Click"/>
                <asp:Button ID="btn_next" runat="server" Text="Next" 
                    onclick="btn_next_Click" />
                <asp:Button ID="btn_forward" runat="server" Text="Last &quot;em falta&quot;" 
                    onclick="btn_forward_Click"/>
            </td>
            <td>
                <asp:Button ID="btn_show" runat="server" Text="Show"/>
                <asp:Button ID="btn_clear" runat="server" Text="Clear"/>
                <asp:Button ID="btn_insert" runat="server"  Text="Insert"/>
                
                <asp:Label ID="lbl_info" runat="server"></asp:Label>
          
            </td>
           
        </tr>
        <tr>
            <td class="style1">
                &nbsp;</td>
            <td>
                &nbsp;</td>
           
        </tr>
        <tr>
            <td class="style1">
                <asp:GridView ID="GridView1" runat="server" 
                    onselectedindexchanged="GridView1_SelectedIndexChanged" 
                    onrowdeleting="GridView1_RowDeleting" Width="343px">
                    <Columns>
                        <asp:CommandField HeaderText="Select" ShowHeader="True" 
                            ShowSelectButton="True" />
                        <asp:ButtonField ButtonType="Button" CommandName="Delete" Text="Delete" />
                    </Columns>
                </asp:GridView>
            </td>
            <td>
                &nbsp;</td>
           
        </tr>
    </table>
</asp:Content>
